#!/bin/bash

# 功能：编译应用文件并复制到tftp目录中
# 用法：./BuildAppAndCopy.sh xxx.c
#

if [ $# -ne 1 ]; then
    echo "Usage: \$0 <source_file.c>"
    exit 1
fi

SOURCE_FILE=$1
EXEC_FILE=$(basename "$SOURCE_FILE" .c)

if [ ! -f "$SOURCE_FILE" ]; then
    echo "Error: Source file '$SOURCE_FILE' not found."
    exit 1
fi


TARGET_DIR="/home/nixiak/linux/tftp"


if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Target directory '$TARGET_DIR' does not exist."
    exit 1
fi


echo "Compiling '$SOURCE_FILE' to '$EXEC_FILE' using arm-linux-gnueabihf-gcc..."
arm-linux-gnueabihf-gcc "$SOURCE_FILE" -o "$EXEC_FILE"
if [ $? -ne 0 ]; then
    echo "Error: Compilation failed."
    exit 1
fi


echo "Copying '$EXEC_FILE' to '$TARGET_DIR'..."
cp "$EXEC_FILE" "$TARGET_DIR"
if [ $? -ne 0 ]; then
    echo "Error: Failed to copy executable to '$TARGET_DIR'."
    exit 1
fi


echo "Compilation successful! '$EXEC_FILE' has been copied to '$TARGET_DIR'."

exit 0
